const domainName = getApp().globalData.domainName
const oss_domain = getApp().globalData.oss_domain
const parameter = getApp().globalData.parameter

  // 操作选项
  const show_opt=(that,e)=>{
    let jg_role_user_id = e.currentTarget.id
    that.setData({
      sel_index:jg_role_user_id,
    })
    wx.showActionSheet({
      itemList: ['修改备注/称呼','删除'],
      itemColor: '#999',
      success: res => {
        if(res.tapIndex==0){
          update_remark(that,jg_role_user_id)
        }else if(res.tapIndex==1){
          delete_role(that,jg_role_user_id)
        }
      },
    })
  }


    // 收起角色列表
const hide_list=(that,e)=>{
      let index = e.currentTarget.id
      let hide_list = that.data.hide_list
      let list_index = hide_list.indexOf(index)
      if(list_index>-1){
        hide_list.splice(list_index,1)
      }else{
        hide_list.push(index)
      }
      that.setData({
        hide_list:hide_list
      })
    }
  
const role_list = ['教学负责人','教务','实践教员','理论教员']
  // 加载班主任列表
const page_init=(that,jg_id,user_id)=>{
    wx.request({
      url: domainName + '/jg_job_init',
      data: {
        jg_id:jg_id,
        user_id:user_id,
      },...parameter,
      success: res => {
        that.setData({
          role_id_list:res.data.role_id_list,
          teaching_director_list:res.data.teaching_director_list,
          registrar_list:res.data.registrar_list,
          practice_ins_list:res.data.practice_ins_list,
          theory_ins_list:res.data.theory_ins_list,
        })
      },
    })
  }
    // 添加机构管理员
const add_jg_admin=(that,e)=>{
  let index = e.currentTarget.id
  wx.showModal({
    editable: true,
    placeholderText: '请输入对方微信授权手机号',
    showCancel: true,
    title: '添加机构的【' + role_list[index-1] + '】',
    success: res => {
      if(res.confirm){
        let phone = res.content
        if(phone.length!=11){
          wx.showToast({
            title: '请正确填写！',
            duration: 1500,
            icon: 'error',
          })
          return 
        }
        wx.request({
          url: domainName + '/add_jg_admin',
          data: {
            role_id:index,
            jg_id:that.jg_id,
            phone:phone,
          },...parameter,
          success: res => {
            if(res.data.row_id == 'nobody'){
              wx.showToast({
                title: '查无此人',
                duration: 1500,
                icon: 'error',
              })
            }else if(res.data.row_id){
              wx.showToast({
                title: '添加完成',
                duration: 1500,
                icon: 'success',
              })
              page_init(that,that.jg_id, that.user_id)
            }
          },
        })
      }
    },
  })

}
// 修改员工备注
const update_remark=(that,jg_role_user_id)=>{
  wx.showModal({
    editable: true,
    placeholderText: '请输入备注/称呼',
    showCancel: true,
    title: '修改备注/称呼',
    success: res => {
      if(res.confirm){
        let content = res.content
        wx.request({
          url: domainName + '/update_jg_role_user_remark',
          data: {
            jg_role_user_id:jg_role_user_id,
            remark:content,
          },...parameter,
          success: res => {
            if(res.data.update_res){
              wx.showToast({
                title: '修改完成',
                duration: 1500,
                icon: 'success',
              })
              page_init(that, that.jg_id,that.user_id)
            }
          },
        })
      }
    },
  })
}
// 删除员工
const delete_role=(that, jg_role_user_id)=>{
  wx.showModal({
    content: '确定删除吗？',
    showCancel: true,
    title: '提示',
    success: res => {
      if(res.confirm){
        wx.showLoading()
        wx.request({
          url: domainName + '/delete_jg_role_user',
          data: {
            jg_role_user_id:jg_role_user_id,
          },...parameter,
          success: res => {
            wx.hideLoading()
            if(res.data.delete_res){
              wx.showToast({
                title: '删除完成！',
                duration: 1500,
                icon: 'success',
              })
              page_init(that, that.jg_id, that.user_id)
            }
          },
        })
      }
    },
  })
}
const user_id = wx.getStorageSync('user_id')

// pages/mana_class_teacher/mana_class_teacher.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_url:oss_domain + 'static/picture/app_icon/',
    hide_list:[],
    role_id_list:[],
    registrar_list:[]
  },
  add_jg_admin(e){add_jg_admin(this,e)},
  show_opt(e){show_opt(this,e)},
  hide_list(e){hide_list(this,e)},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.jg_id = options.jg_id
    this.user_id = user_id
    page_init(this,this.jg_id,user_id)
  },

})